// ml278
// Copyright (C) 2008 Bjørn Øivind Bjørnsen, Aleksander Gjermundsen, Knut Andre Tidemann, Thomas Hansen
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

#include "planetarytile.h"
#include "planetaryrenderable.h"
#include "planetaryscenemanager.h"
#include "planetarypage.h"
#include "planetaryoptions.h"

#include <OgreStringConverter.h>

namespace Ogre
{
	PlanetaryTile::PlanetaryTile(PlanetarySceneManager *mgr) : PlanetaryLoadable(mgr)
	{
		mSceneMgr = mgr;
		mRenderable = NULL;
		mParent = NULL;
		mTileNode = NULL;

		mZone = 0;
		mPageX = 0;
		mPageZ = 0;
		mTileX = 0;
		mTileZ = 0;

		mIsInit = false;
		mIsLoaded = false;
	}

	PlanetaryTile::~PlanetaryTile()
	{
		if(mIsLoaded)
			unload();

		if(mIsInit)
			uninit();
	}

	void PlanetaryTile::init(unsigned int zone, unsigned int pageX, unsigned int pageZ, unsigned int tileX, unsigned int tileZ, PlanetaryPage *parent)
	{
		assert(!mIsInit);

		mZone	= zone;
		mPageX	= pageX;
		mPageZ	= pageZ;
		mTileX	= tileX;
		mTileZ	= tileZ;

		mParent = parent;

		assert(mRenderable == NULL);

		// Create scene node
		if(!mTileNode)
		{
			const String name = "Node::" + StringConverter::toString(mZone) + "::" + 
				StringConverter::toString(mPageX) + "." + StringConverter::toString(mPageZ) + "::" + 
				StringConverter::toString(mTileX) + "." + StringConverter::toString(mTileZ);

			mTileNode = mParent->getSceneNode()->createChildSceneNode(name);
		}

		mRenderable = new PlanetaryRenderable(mSceneMgr);
		mRenderable->init(mZone,mPageX,mPageZ,mTileX,mTileZ,this);

		mIsInit = true;
	}

	void PlanetaryTile::load()
	{
		assert(mIsInit);

		mRenderable->load();
		mRenderable->setVisible(false);

		LogManager::getSingleton().logMessage("Tile loaded: " + StringConverter::toString(mZone) + "::" + 
			StringConverter::toString(mPageX) + "." + StringConverter::toString(mPageZ) + "::" + 
			StringConverter::toString(mTileX) + "." + StringConverter::toString(mTileZ));

		mIsLoaded = true;
	}

	void PlanetaryTile::unload()
	{
		assert(mIsLoaded);

		if(mRenderable->isVisible())
			show(false);

		mRenderable->unload();

		LogManager::getSingleton().logMessage("Page unloaded: " + StringConverter::toString(mZone) + "::" + 
			StringConverter::toString(mPageX) + "." + StringConverter::toString(mPageZ));

		mIsLoaded = false;
	}

	void PlanetaryTile::uninit()
	{
		assert(mIsInit);

		mZone = 0;
		mPageX = 0;
		mPageZ = 0;
		mTileX = 0;
		mTileZ = 0;

		mRenderable->uninit();
		delete mRenderable;

		mTileNode->removeAndDestroyAllChildren();
		mIsInit = false;
	}

	void PlanetaryTile::show(bool do_it)
	{
		if(do_it)
		{
			mRenderable->setMaterial(mParent->getMaterial());
			mRenderable->setVisible(true);
			mTileNode->attachObject(mRenderable);
		}
		else
		{
			mRenderable->setVisible(false);
			mTileNode->detachObject(mRenderable);
		}
	}

	void PlanetaryTile::setMaterial(MaterialPtr mat)
	{
		mRenderable->setMaterial(mat);
	}
}
